<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../Universal.css">
</head>
<body>
    
    <div id="cnblogs_post_body" class="blogpost-body">
        <h2>DBUtils</h2>
    <p>如果只使用JDBC进行开发，我们会发现冗余代码过多，为了简化JDBC开发，本案例我们讲采用apache commons组件一个成员：DBUtils。DBUtils就是JDBC的简化开发工具包。需要项目导入commons-dbutils-1.6.jar才能够正常使用DBUtils工具。</p>
    <p>DBUtils是java编程中的数据库操作实用工具，小巧简单实用。</p>
    <p>DBUtils封装了对JDBC的操作，简化了JDBC操作，可以少写代码。</p>
    <h3>Dbutils三个核心功能介绍</h3>
    <ul>
    <li>QueryRunner中提供对sql语句操作的API</li>
    <li>ResultSetHandler接口，用于定义select操作后，怎样封装结果集</li>
    <li>DbUtils类，它就是一个工具类,定义了关闭资源与事务处理的方法</li>
    </ul>
    <h3>QueryRunner核心类</h3>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">update</span><span style="color: rgba(0, 0, 0, 1)">(Connection conn, String sql, Object... params) ，用来完成表数据的增加、删除、更新操作
    query(Connection conn, String sql, ResultSetHandler</span><span style="color: rgba(128, 128, 128, 1)">&lt;</span>T<span style="color: rgba(128, 128, 128, 1)">&gt;</span> rsh, Object... params) ，用来完成表数据的查询操作</pre>
    </div>
    <h3>QueryRunner实现添加、更新、删除操作</h3>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">update</span>(Connection conn, String sql, Object... params) ，用来完成表数据的增加、删除、更新操作</pre>
    </div>
    <p>增删改代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.QueryRunner;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.Connection;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.SQLException;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * Created by Administrator on 2017/7/20.
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Demo01 {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> insert() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取一个用来执行SQL语句的对象 QueryRunner</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner();
    
                String sql </span>= "INSERT INTO zhangwu(name,money,parent) VALUES(?,?,?)"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>= {"股票收入", 5500, "收入"<span style="color: rgba(0, 0, 0, 1)">};
                Connection conn </span>=<span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0.getConnection();
                </span><span style="color: rgba(0, 0, 255, 1)">int</span> line = qr.update(conn, sql, params);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 用来完成表数据的增加、删除、更新操作
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集处理</span>
                System.out.println("line = " +<span style="color: rgba(0, 0, 0, 1)"> line);
    
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);
            }
        }
        
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">更新</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> update() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个QueryRunner对象，用来完成SQL语句的执行</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "UPDATE zhangwu SET money = money+1000 WHERE name=?"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>= {"股票收入"<span style="color: rgba(0, 0, 0, 1)">};
                Connection conn </span>=<span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0.getConnection();
                </span><span style="color: rgba(0, 0, 255, 1)">int</span> line =<span style="color: rgba(0, 0, 0, 1)"> qr.update(conn, sql, params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集的处理</span>
                System.out.println("line=" +<span style="color: rgba(0, 0, 0, 1)"> line);
    
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);
            }
        }
        
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> delete() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个QueryRunner对象，用来完成SQL语句的执行</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "DELETE FROM zhangwu WHERE name = ?"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>= {"股票收入"<span style="color: rgba(0, 0, 0, 1)">};
                Connection conn </span>=<span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0.getConnection();
                </span><span style="color: rgba(0, 0, 255, 1)">int</span> line =<span style="color: rgba(0, 0, 0, 1)"> qr.update(conn, sql, params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集的处理</span>
                System.out.println("line=" +<span style="color: rgba(0, 0, 0, 1)"> line);
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);
            }
        }
    }</span></pre>
    </div>
    <pre>JDBCUtils0类：</pre>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
     *  实现JDBC的工具类
     *  定义方法,直接返回数据库的连接对象
     *
     *  写关闭方法
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    
    <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.Connection;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.DriverManager;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.ResultSet;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.SQLException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.Statement;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0 {
        </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0(){}
        </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Connection con ;
    
        </span><span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)">{
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                Class.forName(</span>"com.mysql.jdbc.Driver"<span style="color: rgba(0, 0, 0, 1)">);
                String url </span>= "jdbc:mysql://localhost:3306/mybase"<span style="color: rgba(0, 0, 0, 1)">;
                String username</span>="root"<span style="color: rgba(0, 0, 0, 1)">;
                String password</span>="123456"<span style="color: rgba(0, 0, 0, 1)">;
                con </span>=<span style="color: rgba(0, 0, 0, 1)"> DriverManager.getConnection(url, username, password);
            }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(Exception ex){
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> RuntimeException(ex+"数据库连接失败"<span style="color: rgba(0, 0, 0, 1)">);
            }
        }
    
        </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
         * 定义静态方法,返回数据库的连接对象
         </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Connection getConnection(){
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> con;
        }
    
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> close(Connection con,Statement stat){
    
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(stat!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
                </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                    stat.close();
                }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(SQLException ex){}
            }
    
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(con!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
                </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                    con.close();
                }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(SQLException ex){}
            }
    
        }
    
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> close(Connection con,Statement stat , ResultSet rs){
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(rs!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
                </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                    rs.close();
                }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(SQLException ex){}
            }
    
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(stat!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
                </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                    stat.close();
                }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(SQLException ex){}
            }
    
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(con!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
                </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                    con.close();
                }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(SQLException ex){}
            }
    
        }
    }</span></pre>
    </div>
    <h3>QueryRunner实现查询操作</h3>
    <div class="cnblogs_code">
    <pre>query(Connection conn, String sql, ResultSetHandler&lt;T&gt; rsh, Object... params) ，用来完成表数据的查询操作</pre>
    </div>
    <h3>ResultSetHandler结果集处理类</h3>
    <table>
    <tbody>
    <tr>
    <td valign="top" width="122">
    <p>ArrayHandler</p>
    </td>
    <td valign="top" width="496">
    <p>将结果集中的第一条记录封装到一个Object[]数组中，数组中的每一个元素就是这条记录中的每一个字段的值</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="122">
    <p>ArrayListHandler</p>
    </td>
    <td valign="top" width="496">
    <p>将结果集中的每一条记录都封装到一个Object[]数组中，将这些数组在封装到List集合中。</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="122">
    <p>BeanHandler</p>
    </td>
    <td valign="top" width="496">
    <p>将结果集中第一条记录封装到一个指定的javaBean中。</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="122">
    <p>BeanListHandler</p>
    </td>
    <td valign="top" width="496">
    <p>将结果集中每一条记录封装到指定的javaBean中，将这些javaBean在封装到List集合中</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="122">
    <p>ColumnListHandler</p>
    </td>
    <td valign="top" width="496">
    <p>将结果集中指定的列的字段值，封装到一个List集合中</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="122">
    <p>ScalarHandler</p>
    </td>
    <td valign="top" width="496">
    <p>它是用于单数据。例如select count(*) from 表操作。</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="122">
    <p>MapHandler</p>
    </td>
    <td valign="top" width="496">
    <p>将结果集第一行封装到Map集合中,Key 列名, Value 该列数据</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="122">
    <p>MapListHandler</p>
    </td>
    <td valign="top" width="496">
    <p>将结果集第一行封装到Map集合中,Key 列名, Value 该列数据,Map集合存储到List集合</p>
    </td>
    </tr>
    </tbody>
    </table>
    <h3>JavaBean</h3>
    <p>JavaBean就是一个类，在开发中常用封装数据。具有如下特性</p>
    <ul>
    <li>1.需要实现接口：java.io.Serializable ，通常实现接口这步骤省略了，不会影响程序。</li>
    <li>2.提供私有字段：private 类型 字段名;</li>
    <li>3.提供getter/setter方法：</li>
    <li>4.提供无参构造</li>
    </ul>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    * 账务类
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ZhangWu {
        </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String name;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">double</span><span style="color: rgba(0, 0, 0, 1)"> money;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String parent;
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> ZhangWu() {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getId() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setId(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">double</span><span style="color: rgba(0, 0, 0, 1)"> getMoney() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> money;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setMoney(<span style="color: rgba(0, 0, 255, 1)">double</span><span style="color: rgba(0, 0, 0, 1)"> money) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.money =<span style="color: rgba(0, 0, 0, 1)"> money;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getParent() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> parent;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setParent(String parent) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.parent =<span style="color: rgba(0, 0, 0, 1)"> parent;
        }
    
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> String toString() { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该方法可以省略</span>
            <span style="color: rgba(0, 0, 255, 1)">return</span> "ZhangWu [id=" + id + ", name=" + name + ", money=" + money + ", parent=" + parent + "]"<span style="color: rgba(0, 0, 0, 1)">;
        }
    }</span></pre>
    </div>
    <h3>ArrayHandler与ArrayListHandler查询</h3>
    <p>ArrayHandler：将结果集中的第一条记录封装到一个Object[]数组中，数组中的每一个元素就是这条记录中的每一个字段的值</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.QueryRunner;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.handlers.ArrayHandler;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.Connection;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.SQLException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Arrays;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayHandlerDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method(){
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取QueryRunner对象</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "SELECT * FROM zhangwu"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>=<span style="color: rgba(0, 0, 0, 1)"> {};
                Connection conn </span>=<span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0.getConnection();
                Object[] objArray </span>= qr.query(conn, sql, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ArrayHandler(), params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集的处理</span>
    <span style="color: rgba(0, 0, 0, 1)">            System.out.println( Arrays.toString(objArray) );
                conn.close();
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[1, 吃饭支出, 247.0]</span></pre>
    </div>
    <p>ArrayListHandler：将结果集中的每一条记录都封装到一个Object[]数组中，将这些数组在封装到List集合中。</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.QueryRunner;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.handlers.ArrayListHandler;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.Connection;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.SQLException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Arrays;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayListHandlerDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取QueryRunner对象</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "SELECT * FROM zhangwu WHERE zmoney&gt;?"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>= {2000<span style="color: rgba(0, 0, 0, 1)">};
                Connection conn </span>=<span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0.getConnection();
                List</span>&lt;Object[]&gt; list = qr.query(conn, sql, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ArrayListHandler(), params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集的处理</span>
                <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Object[] objArray : list) {
                    System.out.println(Arrays.toString(objArray));
                }
    
                conn.close();
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[2, 工资收入, 12345.0]
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[5, 股票收入, 8000.0]
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[6, 打麻将支出, 8000.0]
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[7, null, 5000.0]</span></pre>
    </div>
    <p>BeanHandler与BeanListHandler查询</p>
    <p>BeanHandler ：将结果集中第一条记录封装到一个指定的javaBean中。</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.QueryRunner;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.handlers.BeanHandler;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.Connection;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.SQLException;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> BeanHandlerDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取QueryRunner</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "SELECT * FROM zhangwu WHERE id=?"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>= {1<span style="color: rgba(0, 0, 0, 1)">};
                Connection conn </span>=<span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0.getConnection();
                ZhangWu zw </span>= qr.query(conn, sql, <span style="color: rgba(0, 0, 255, 1)">new</span> BeanHandler&lt;ZhangWu&gt;(ZhangWu.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">), params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集处理</span>
    <span style="color: rgba(0, 0, 0, 1)">            System.out.println(zw);
    
                conn.close();
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);
            }
        }
    }
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZhangWu [id=1, name=null, money=0.0, parent=null]</span></pre>
    </div>
    <p>BeanListHandler ：将结果集中每一条记录封装到指定的javaBean中，将这些javaBean在封装到List集合中</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.QueryRunner;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.handlers.BeanListHandler;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.Connection;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.SQLException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> BeanListHandlerDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取QueryRunner</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "SELECT * FROM zhangwu WHERE zmoney&gt;?"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>= {2000<span style="color: rgba(0, 0, 0, 1)">};
                Connection conn </span>=<span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0.getConnection();
                List</span>&lt;ZhangWu&gt; list = qr.query(conn, sql, <span style="color: rgba(0, 0, 255, 1)">new</span> BeanListHandler&lt;ZhangWu&gt;(ZhangWu.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">), params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集处理</span>
                <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (ZhangWu zw : list) {
                    System.out.println(zw);
                }
    
                conn.close();
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);
            }
        }
    }
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZhangWu [id=2, name=null, money=0.0, parent=null]
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZhangWu [id=5, name=null, money=0.0, parent=null]
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZhangWu [id=6, name=null, money=0.0, parent=null]
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZhangWu [id=7, name=null, money=0.0, parent=null]</span></pre>
    </div>
    <p>ColumnListHandler与ScalarHandler查询</p>
    <p>ColumnListHandler：将结果集中指定的列的字段值，封装到一个List集合中</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.QueryRunner;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.handlers.ColumnListHandler;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.Connection;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.SQLException;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ColumnListHandlerDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method(){
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取QueryRunner对象</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "SELECT zname FROM zhangwu WHERE zmoney&gt;?"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>= {2000<span style="color: rgba(0, 0, 0, 1)">};
                Connection conn </span>=<span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0.getConnection();
                List</span>&lt;String&gt; list = qr.query(conn, sql, <span style="color: rgba(0, 0, 255, 1)">new</span> ColumnListHandler&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">(), params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集的处理</span>
                <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String str : list) {
                    System.out.println(str);
                }
    
                conn.close();
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">工资收入
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">股票收入
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打麻将支出
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">null</span></pre>
    </div>
    <p>ScalarHandler：它是用于单数据。例如select count(*) from 表操作。</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.QueryRunner;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.handlers.ScalarHandler;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.Connection;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.SQLException;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ScalarHandlerDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method(){
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取QueryRunner对象</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner();
    
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "SELECT MAX(zmoney) FROM zhangwu"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>=<span style="color: rgba(0, 0, 0, 1)"> {};
                Connection conn </span>=<span style="color: rgba(0, 0, 0, 1)"> JDBCUtils0.getConnection();
                Double max </span>= qr.query(conn, sql, <span style="color: rgba(0, 0, 255, 1)">new</span> ScalarHandler&lt;Double&gt;<span style="color: rgba(0, 0, 0, 1)">(), params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集的处理</span>
                System.out.println("max=" +<span style="color: rgba(0, 0, 0, 1)"> max);
    
                conn.close();
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">max=12345.0</span></pre>
    </div>
    <h2>连接池</h2>
    <p>实际开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程，为了解决此类性能问题，通常情况我们采用连接池技术，来共享连接Connection。这样我们就不需要每次都创建连接、释放连接了，这些操作都交给了连接池。</p>
    <p>用池来管理Connection，这样可以重复使用Connection。有了池，所以我们就不用自己来创建Connection，而是通过池来获取Connection对象。当使用完Connection后，调用Connection的close()方法也不会真的关闭Connection，而是把Connection“归还”给池。池就可以再利用这个Connection对象了。</p>
    <p>Java为数据库连接池提供了公共的接口：javax.sql.DataSource，各个厂商需要让自己的连接池实现这个接口。这样应用程序可以方便的切换不同厂商的连接池！<br>常见的连接池：DBCP、C3P0。<br>接下来，我们就详细的学习一下DBCP连接池。</p>
    <h3>DBCP连接池 </h3>
    <p>DBCP也是一个开源的连接池，是Apache Common成员之一，在企业开发中也比较常见，tomcat内置的连接池。</p>
    <ul>
    <li>导入jar包&nbsp;&nbsp;<span class="cnblogs_code">commons-dbcp-1.4.jar</span>&nbsp;</li>
    <li>编写工具类</li>
    
    
    </ul>
    <p>连接数据库表的工具类, 采用DBCP连接池的方式来完成，Java中提供了一个连接池的规则接口 ：<br>DataSource ：它是java中提供的连接池，作为 DriverManager 工具的替代项。在DBCP包中提供了DataSource接口的实现类，我们要用的具体的连接池 BasicDataSource类</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
     *  使用DBCP实现数据库的连接池
     *  连接池配置,自定义类,
     *  最基本四项完整
     *  对于数据库连接池其他配置,自定义
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    
    <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbcp.BasicDataSource;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.sql.DataSource;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> JDBCUtils {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建出BasicDataSource类对象</span>
        <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> BasicDataSource datasource = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BasicDataSource();
    
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">静态代码块,对象BasicDataSource对象中的配置,自定义</span>
        <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">数据库连接信息,必须的</span>
            datasource.setDriverClassName("com.mysql.jdbc.Driver"<span style="color: rgba(0, 0, 0, 1)">);
            datasource.setUrl(</span>"jdbc:mysql://localhost:3306/mybase"<span style="color: rgba(0, 0, 0, 1)">);
            datasource.setUsername(</span>"root"<span style="color: rgba(0, 0, 0, 1)">);
            datasource.setPassword(</span>"123"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对象连接池中的连接数量配置,可选的</span>
            datasource.setInitialSize(10);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">初始化的连接数</span>
            datasource.setMaxActive(8);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最大连接数量</span>
            datasource.setMaxIdle(5);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最大空闲数</span>
            datasource.setMinIdle(1);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最小空闲</span>
    <span style="color: rgba(0, 0, 0, 1)">    }<br>
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义静态方法,返回BasicDataSource类的对象</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> DataSource getDataSource() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> datasource;
        }
    }</span></pre>
    </div>
    <h3>工具类的使用</h3>
    <p>测试类</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> jdbc;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.QueryRunner;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.dbutils.handlers.BeanHandler;
    
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.sql.SQLException;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    * 演示使用DBUtils工具 完成数据库表的增加操作
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Demo02 {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 插入功能</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> insert() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取一个用来执行SQL语句的对象 QueryRunner</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner(JDBCUtils.getDataSource());
                String sql </span>= "INSERT INTO zhangwu(zname,zmoney) VALUES(?,?)"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>= {"股票收入", 5500<span style="color: rgba(0, 0, 0, 1)">};
                </span><span style="color: rgba(0, 0, 255, 1)">int</span> line =<span style="color: rgba(0, 0, 0, 1)"> qr.update(sql, params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集处理</span>
                System.out.println("line = " +<span style="color: rgba(0, 0, 0, 1)"> line);
    
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);
            }
        }
    
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除功能</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> delete() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个QueryRunner对象，用来完成SQL语句的执行</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner(JDBCUtils.getDataSource());
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "DELETE FROM zhangwu WHERE zname = ?"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>= {"股票收入"<span style="color: rgba(0, 0, 0, 1)">};
                </span><span style="color: rgba(0, 0, 255, 1)">int</span> line =<span style="color: rgba(0, 0, 0, 1)"> qr.update(sql, params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集的处理</span>
                System.out.println("line=" +<span style="color: rgba(0, 0, 0, 1)"> line);
    
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);
            }
        }
    
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">更新功能</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> update() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个QueryRunner对象，用来完成SQL语句的执行</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner(JDBCUtils.getDataSource());
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "UPDATE zhangwu SET zmoney = zmoney+1000 WHERE zname=?"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>= {"股票收入"<span style="color: rgba(0, 0, 0, 1)">};
                </span><span style="color: rgba(0, 0, 255, 1)">int</span> line =<span style="color: rgba(0, 0, 0, 1)"> qr.update(sql, params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集的处理</span>
                System.out.println("line=" +<span style="color: rgba(0, 0, 0, 1)"> line);
    
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);
            }
        }
    
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询功能,将结果集中第一条记录封装到一个指定的javaBean中。</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> search() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取QueryRunner</span>
                QueryRunner qr = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryRunner(JDBCUtils.getDataSource());
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span>
                String sql = "SELECT * FROM zhangwu"<span style="color: rgba(0, 0, 0, 1)">;
                Object[] params </span>=<span style="color: rgba(0, 0, 0, 1)"> {};
                ZhangWu p </span>= qr.query(sql, <span style="color: rgba(0, 0, 255, 1)">new</span> BeanHandler&lt;ZhangWu&gt;(ZhangWu.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">), params);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">结果集处理</span>
    <span style="color: rgba(0, 0, 0, 1)">            System.out.println(p);
    
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);
            }
        }
    }
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">line = 1
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">line=1
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">line=1
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZhangWu [id=1, name=null, money=0.0, parent=null]</span></pre>
    </div>
    <h3>常见配置项</h3>
    <table>
    <tbody>
    <tr>
    <td valign="top" width="135">
    <p>分类</p>
    </td>
    <td valign="top" width="193">
    <p>属性</p>
    </td>
    <td valign="top" width="290">
    <p>描述</p>
    </td>
    </tr>
    <tr>
    <td rowspan="4" valign="top" width="135">
    <p>必须项</p>
    </td>
    <td valign="top" width="193">
    <p>driverClassName</p>
    </td>
    <td valign="top" width="290">
    <p>数据库驱动名称</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="193">
    <p>url</p>
    </td>
    <td valign="top" width="290">
    <p>数据库的地址</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="193">
    <p>username</p>
    </td>
    <td valign="top" width="290">
    <p>用户名</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="193">
    <p>password</p>
    </td>
    <td valign="top" width="290">
    <p>密码</p>
    </td>
    </tr>
    <tr>
    <td rowspan="4" valign="top" width="135">
    <p>基本项（扩展）</p>
    </td>
    <td valign="top" width="193">
    <p>maxActive</p>
    </td>
    <td valign="top" width="290">
    <p>最大连接数量</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="193">
    <p>minIdle</p>
    </td>
    <td valign="top" width="290">
    <p>最小空闲连接</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="193">
    <p>maxIdle</p>
    </td>
    <td valign="top" width="290">
    <p>最大空闲连接</p>
    </td>
    </tr>
    <tr>
    <td valign="top" width="193">
    <p>initialSize</p>
    </td>
    <td valign="top" width="290">
    <p>初始化连接</p>
    </td>
    </tr>
    </tbody>
    </table>
    <p>参考文档：http://commons.apache.org/proper/commons-dbcp/configuration.html</p>
    </div>
    
</body>
</html>